<?php
defined('BASEPATH') OR exit('No direct script access allowed');
use EasyWeChat\Foundation\Application;

class Product extends FOX_Controller {

	function __construct ()
	{
		parent::__construct();
		$this->load->model('good_m');
		$this->load->model('shop_m');
		$this->load->model('api_good_m');
		$this->load->model('areas_m');
		$this->load->model('member_m');
        $this->load->model('wxUser_m');
        $this->load->model('category_m');
		$this->load->model('goodVouchers_m');
		$this->load->model('attachs_m');
		$this->load->model('shareViewLogs_m');
        $this->load->model('shareGoodRecord_m');
        $this->load->model('pay_m');
        $this->load->model('aliorder_m');
	}
	public function index(){
		$web_category = $this->uri->segment(3);
		$data['categorys'] = $this->category_m->get_cates_by_all_category(1);
		$data['total_rows'] = $this->api_good_m->get_goods_list_count(1);
		$data['goods'] = $this->api_good_m->get_goods_list_near(0,$this->config->item('page_list_home'),1,$this->USER_LAT,$this->USER_LNG);
		if($data['goods']){
			foreach($data['goods'] as $k => $v){
				$data['goods'][$k]['count_orders'] = $this->api_good_m->count_shop_orders_count_by_good_id($v['good_id']);
				//获取图片
				$pic_lists = $this->attachs_m->get_one_attachs_by($v['good_cover']);
				if($pic_lists){
					$data['goods'][$k]['good_cover'] = get_jianlou_image($pic_lists['file_location'],$pic_lists['file_name'],"big");
				}else{
					$data['goods'][$k]['good_cover'] = base_url('uploads/goods/nopic.jpg');
				}
			}
		}
		$data['foot_memu'] = 'near';
		$this->load->view('nearby_list',$data);
	}
	public function goods_show($id=0,$referee=0,$order_msg=1000,$order_id=0,$order_price=0){
		$user_id=$this->session->userdata('user_id');
		$data['order_info'] = array();
		if($order_id>0 && $user_id){
			if($order = $this->aliorder_m->get_shop_orders_by_id($order_id)){
				if($user_id==$order['user_id']){
					$data['order_info'] = $order;
				}				
			}
		}
		$good_id =  $this->uri->segment(3);
		$favorite = $this->member_m->is_favorites($user_id,$good_id);
		$data['favorite'] = $favorite;
		if(!is_numeric($id)){
			exit();
		}
		$data['good_info'] = $this->good_m->get_shop_goods_by_id($id);		
		if(!$data['good_info']){
			exit();
		}
		$wxreferee = $this->input->get('wxreferee');
        if(isset($wxreferee) && !empty($wxreferee)){
            $referee=$wxreferee;
        }
		//种下推广COOKIES
		if($referee>0){
			if($refereeinfo = $this->user_m->get_user_by_user_id($referee)){
				$_COOKIE['user_referee'] = $referee;
				setcookie('user_referee', $referee, time() + 86500, '/', '', false, true);
			}
		}
		$this->db->set('good_view',$data['good_info']['good_view']+1,false)->where('good_id',$data['good_info']['good_id'])->update('shop_goods');
		$this->session->set_userdata('gourl', site_url('product/goods_show/'.$id));
		$data['shop_info'] = $this->shop_m->get_shop_user_by_id($data['good_info']['shop_id']);
		$data['good_info']['count_orders'] = $this->api_good_m->count_shop_orders_count_by_good_id($data['good_info']['good_id']);
		//获取图片
        $data['good_info']['pic_lists'] = $this->attachs_m->get_attachs_by($data['good_info']['good_cover']);
		$avg_orders = $this->api_good_m->avg_shop_orders_count_by_good_id($data['good_info']['good_id']);
		if($avg_orders['order_price']>0){
			$data['good_info']['avg_orders'] = $avg_orders['order_price'];
		}else{
			$data['good_info']['avg_orders'] = $data['good_info']['good_price'];
		}
		if(isset($data['shop_info'])){
			$area =  $this->areas_m->get_area_by_id($data['shop_info']['shop_province']);
			$area2 =  $this->areas_m->get_area_by_id($data['shop_info']['shop_city']);
			$area3 =  $this->areas_m->get_area_by_id($data['shop_info']['shop_district']);
			$data['shop_info']['shop_one'] = cut_dress($area['area_name'],$area2['area_name']).$area3['area_name'];				
		}
		if($data['good_info']['good_voucher'] == 1){
			$vouchers = $this->goodVouchers_m->get_good_vouchers_by($data['good_info']['good_id']);
			if(!empty($vouchers)){
				foreach ($vouchers as $key => $value){
					$vouchers_id_arr[] = $value['voucher_type'];
					$vouchers_kv[$value['voucher_type']] = $value['voucher_tnum'];
				}
				$data['voucher_ids'] = $vouchers_id_arr;
			}else{
				$data['voucher_ids'] = [];
			}
		}
		$data['vouchers_kv'] = !empty($vouchers_kv)?$vouchers_kv:[];
		$arr['user_id'] = $this->session->userdata('user_id');
		$arr['item_id'] = $this->uri->segment(3);
		$arr['item_type'] = 0;
		$arr['web_category'] = $data['good_info']['web_category'];
		$arr['view_time'] = time();
		$arr['view_num'] = 1;
		$isfootprint = $this->member_m->is_footprint($user_id,$good_id);
		// pp($isfootprint);exit;
		if($isfootprint){
			$view_id = $isfootprint['view_id'];
			$footprint = $this->member_m->updates_goods_footpoint($view_id);
			
		}else{
			$footprint = $this->member_m->add_goods_footpoint($arr);
		}
        $data['openid'] = !empty($this->session->userdata("wechat_user")['id']) ? $this->session->userdata("wechat_user")['id'] : "0";
		$data['order_msg']=$order_msg;
		$data['order_id']=$order_id;
		$data['order_price']=$order_price;
		
		if(!empty($this->input->get('ref'))){
			$data['ref']=1;
		}else{
			$data['ref']=0;
		}
		// 个人分享链接
		if($this->session->userdata('user_id')){
			$good_link = site_url("product/goods_show/".$id."?wxreferee=".$this->session->userdata('user_id'));
		}else{
			$good_link = site_url("product/goods_show/".$id);
		}
        
        $data['good_link']=$good_link;
        $data['good_pic']=get_jianlou_image($data['good_info']['pic_lists'][0]['file_location'],$data['good_info']['pic_lists'][0]['file_name'],"small");
		// 微信浏览器自定义分享
        if(strstr($_SERVER['HTTP_USER_AGENT'],'MicroMessenger')){
            $config = [
                'app_id' => config_item("app_id"),
                'secret' => config_item("app_secret"),
            ];
            $app = new Application($config);
            $js = $app->js;
            $data['js']=$js;
            $data['openid']=$this->session->userdata("wechat_user")['id'];
            // 维信支付目录只能是一级
            $data['good_link']=site_url("product/goods_show/".$id."?wxreferee=".$this->session->userdata('user_id'));
        }
        if(!empty($referee)){
            if($user_id != $referee){
                // 绑定上下级关系
                $userinfo2=$this->user_m->get_user_by_user_id($user_id);
                if(empty($userinfo2['user_referee'])){
                    $this->user_m->update_user_referee($user_id,$referee);
                }
                //商品分享浏览量加一
                $share_info = $this->shareGoodRecord_m->get_share_record_limit($referee,$id);
                if(!empty($share_info)){
                    // 点击记录
                    $log_info = $this->shareViewLogs_m->get_share_log_limit($user_id,$id);
                    if(empty($log_info)){
                        $logs = array(
                            'user_id'=>$user_id,
                            'good_id'=>$id,
                            'referee_share_id'=>$referee,
                            'ctime'=>date("Y-m-d H:i:s",time()),
                        );
                        $this->shareViewLogs_m->add_share_view_logs($logs);
                        $this->shareGoodRecord_m->set_int_click_count($share_info['id']);
                    }

                }
            }


        }
        $data['referee']=$referee;
        //pp($data);exit;
		$this->load->view('goods_show',$data);
	}

	//成交
	public function gods()
	{
		$data['categorys'] = $this->category_m->get_cates_by_all_category(1);
		$data['total_rows'] = $this->api_good_m->get_goods_list_count(1);
		$data['goods'] = $this->api_good_m->get_goods_list_gods(0,$this->config->item('page_list_home'),1,'desc');
		if($data['goods']){
			foreach($data['goods'] as $k => $v){
				$data['goods'][$k]['count_orders'] = $this->api_good_m->count_shop_orders_count_by_good_id($v['good_id']);
				//获取图片
				$pic_lists = $this->attachs_m->get_one_attachs_by($v['good_cover']);
				if($pic_lists){
					$data['goods'][$k]['good_cover'] = get_jianlou_image($pic_lists['file_location'],$pic_lists['file_name'],"big");
				}else{
					$data['goods'][$k]['good_cover'] = base_url('uploads/goods/nopic.jpg');
				}
			}
		}
		$this->load->view('gods_list',$data);
	}

	public function lists(){
		$web_category = $this->uri->segment(3);
		$data['categorys'] = $this->category_m->get_cates_by_all_category(1);
		$data['total_rows'] = $this->api_good_m->get_goods_list_count(1);
		$data['goods'] = $this->good_m->get_all_shop_goods();
		$this->load->view('products_lists',$data);
	}

}
